package com.app.micaihu.view.main.game.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.app.micaihu.R;
import com.app.micaihu.adapter.PagerAdapter.CommonFragmentAdapter;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.base.BaseOldFragment;
import com.app.micaihu.configure.Config;
import com.app.utils.stickheaderview.tab.SlidingTabLayout;

import java.util.ArrayList;

/**
 * Created by Guo on 2017/5/22.
 *
 * 游戏排行的Fragment
 *
 */

public class GameRankPageFragment extends BaseOldFragment implements ViewPager.OnPageChangeListener{

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 1:
                    GameRankingFragment fragment = (GameRankingFragment) fragments.get(position);
                    if(fragment != null){
                        fragment.notifyDown();
                    }

                    break;
            }


        }
    };


    /********
     * 页面控件
     **********/
    private View mContextView;
    private ViewPager mViewpager;

    private String[] titls = {AppApplication.getInstance().getString(R.string.game_rank_popul),
            AppApplication.getInstance().getString(R.string.game_rank_new),
            AppApplication.getInstance().getString(R.string.game_rank_month)
    };
    /***********
     * 逻辑数据
     ***********/
    private ArrayList<Fragment> fragments = new ArrayList<Fragment>();//新闻页面集合
    private SlidingTabLayout navTab;
    private int position;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (mContextView == null) {
            mContextView = inflater.inflate(R.layout.fragment_gameranking, null);
            mViewpager = (ViewPager) mContextView.findViewById(R.id.mViewPager);
            navTab = (SlidingTabLayout) mContextView.findViewById(R.id.navig_tab);
            initView();
        }

        return mContextView;
    }


    @Override
    public void onResume() {
        super.onResume();
        flushDownProgress(position);
    }

    private void flushDownProgress(int position) {
//        if (fragments != null && fragments.size() > position && DownloadUtils.DOWNLOADED != 0) {
//            handler.sendEmptyMessageDelayed(1,300);
//        }
    }


    /**
     * 初始化布局
     */
    private void initView() {
        fragments.clear();
//        1流行榜 2新品榜 3月度榜 4单机榜 5网游榜
        for (int i = 0; i < 3; i++) {
            GameRankingFragment gameRankingFragment = new GameRankingFragment();
            Bundle bundle = new Bundle();
            if (i == 0) {
                bundle.putString(Config.Extra.PARAMETER1, "1");
            } else if (i == 1) {
                bundle.putString(Config.Extra.PARAMETER1, "2");
            } else if (i == 2) {
                bundle.putString(Config.Extra.PARAMETER1, "3");
            }

            gameRankingFragment.setArguments(bundle);
            fragments.add(gameRankingFragment);
        }

        CommonFragmentAdapter commonFragmentAdapter = new CommonFragmentAdapter(getChildFragmentManager(), fragments, titls);

        mViewpager.setAdapter(commonFragmentAdapter);
        mViewpager.setOffscreenPageLimit(fragments.size());
        navTab.setViewPager(mViewpager);
        navTab.setOnPageChangeListener(this);
        mViewpager.setCurrentItem(0);
        navTab.setTabSelected(0);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        this.position = position;

        flushDownProgress(position);
    }


    @Override
    public void onPageScrollStateChanged ( int state){

    }



}
